package org.apache.jmeter.assertions.gui;

import java.awt.BorderLayout;



import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.apache.jmeter.gui.util.VerticalPanel;
import org.apache.jmeter.gui.util.HorizontalPanel;

import org.apache.jmeter.assertions.gui.AbstractAssertionGui;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.assertions.JSONAssertion;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger;

public class JSONAssertionGui extends AbstractAssertionGui implements ChangeListener {
	
    private static final Logger log = LoggingManager.getLoggerForClass();

    private static final long serialVersionUID = 1L;
    
    private JCheckBox isCheck = null;
    private JTextField jsonPath = null;
    private JTextField expectedData = null;
    
    public JSONAssertionGui(){
    	init();
    }
    
    private void init(){
    	setLayout(new BorderLayout());
        setBorder(makeBorder());

        add(makeTitlePanel(), BorderLayout.NORTH);
        
        JPanel mainPanel = new JPanel(new BorderLayout());
        HorizontalPanel subPanel = new HorizontalPanel();
        subPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Settings"));
        
        //HorizontalPanel Panel1 = new HorizontalPanel();
        
        jsonPath = new JTextField(1);
        jsonPath.setText("$.");
        jsonPath.setName("JsonPathField");
        subPanel.add(new JLabel("JSON_Path:"));
        subPanel.add(jsonPath);
        
        isCheck = new JCheckBox();
        isCheck.addChangeListener(this);
        subPanel.add(new JLabel("CheckResult:"));
        subPanel.add(isCheck);
        
        expectedData = new JTextField(1);
        expectedData.setName("ExpectedDataField");
        expectedData.setEnabled(false);
        subPanel.add(new JLabel("Expected_Data:"));
        subPanel.add(expectedData);
        
        mainPanel.add(subPanel,BorderLayout.NORTH);
        
        add(mainPanel,BorderLayout.CENTER);
        
    }
    
    @Override
    public void configure(TestElement element){
    	super.configure(element);
    	JSONAssertion el = (JSONAssertion)element;
    	jsonPath.setText(el.getJsonpath());
    	isCheck.setSelected(el.Ischeck());
    	expectedData.setText(el.getExpecteddata());
    }
    
    @Override
    public void clearGui(){
    	super.clearGui();
    	jsonPath.setText("$.");
    	isCheck.setSelected(false);
    	expectedData.setText("");
    }

	@Override
	public String getLabelResource() {
		// TODO Auto-generated method stub
		return "json_assertion_title";
	}

    @Override
    public String getStaticLabel(){
        return "JSON Assertion";
    }

	@Override
	public TestElement createTestElement() {
		// TODO Auto-generated method stub
		JSONAssertion element = new JSONAssertion();
		modifyTestElement(element);
		return element;
	}

	@Override
	public void modifyTestElement(TestElement element) {
		// TODO Auto-generated method stub
		log.debug("JSONAssertion.modifyTestElement() called");
		super.configureTestElement(element);
		if (element instanceof JSONAssertion){
			JSONAssertion el = (JSONAssertion)element;
			el.setJsonpath(jsonPath.getText());
			el.setIscheck(isCheck.isSelected());
			el.setExpecteddata(expectedData.getText());
		}
	}

	@Override
	public void stateChanged(ChangeEvent arg0) {
		// TODO Auto-generated method stub
		expectedData.setEnabled(isCheck.isSelected());
	}



}
